require"common"

--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )
        if len%2 ~= 0 then
                return nil
        end

        for i=1,len,2 do
                c = string.sub(num,i,i+1)
                if tonumber(c) > max or tonumber(c) <1 then return nil end
                if not in_table( c, st ) then
                        table.insert( st, c )
                else
                        return nil
                end
        end

        return true
end

function in_table ( e, t )
        for _,v in pairs(t) do
                if (v==e) then return true end
        end
        return false
end

--=======================返回注数,倍投数 , 投注串, 指令串====================================
function random_122( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( inStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  2#注数#B倍数
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12)
			zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_122('1', zlStr)
	end
	return butNum, butPower, butStr, zlStr
end

--======================================返回注数,倍投数,投注串==============================
function check_122( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式自选 12#注数
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 4 then return 0 end
			if  not judge_str(xItem[i],12) then return 0 end
		end
	--复式自选  123#注码  123#02040607
	elseif butType == '2' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 1 or string.len(xItem[1])/2 < 3 then return 0 end
		if  not judge_str(xItem[1],12) then return 0 end
		butNum = combination(string.len(xItem[1])/2,2)
	else
		return 0
	end
	return butNum,butPower,inStr,inStr
end

function query_122(butType,queryStr,djStr)
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local result = 0
	local itemLen = 0;
	itemList = Split( queryStr, "#B" )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	jItem = Split(itemList[1],"#")

	if butType == '1' then
		result = MultSimpCompute(jItem,djStr,butPower,2,2,butType)
	elseif butType == '2' then
		bingoNum = 0
		for i = 1,string.len(djStr), 2 do
			if string.find(queryStr, string.sub(djStr,i, i + 1)) then
				bingoNum = bingoNum + 1
			end
		end
		if bingoNum == 2 then
			result = butPower
		end
	else
		return 0
	end
	return result
end
--print(random_122('1',"5#B11"))
--print(check_122('1',"0102#0304#B10"))
--print(check_122('2',"01020304050607#B10"))
--print(query_122('1',"0102#0304#B10","0102"))
